package model;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class SalesForceIO {
    public String ServerNaam, username, passsword;
    private ResultSet rst;
    private Connection con;
    private Statement stmt;

  /*  public SalesForceIO() {
	super();
	try {
	    Class.forName("com.ddtek.jdbc.sforce.SForceDriver").newInstance();
	} catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) {
	    System.out.println("Driver kan niet geladen worden \n");
	    e.printStackTrace();
	}

    }*/

    public void makeCacheSFContact() {
	connectUrlMaken();
//	CREATE CACHE ON {remote_table}
//	[REFERENCING (remote_table_ref[,remote_table_ref]...)]
//	[REFRESH_INTERVAL {0 | -1 | interval_value [{M, H, D}]}]
//	[INITIAL_CHECK [{ONFIRSTCONNECT | FIRSTUSE | DEFAULT}]
//	[PERSIST {TEMPORARY | MEMORY | DISK | DEFAULT}]
//	[ENABLED {YES | TRUE | NO | FALSE}]
//	[CALL_LIMIT {0 | -1 | max_calls}]
//	[FILTER (expression)]CREATE CACHE ON {remote_table}
//	[REFERENCING (remote_table_ref[,remote_table_ref]...)]
//		[REFRESH_INTERVAL {0 | -1 | interval_value [{M, H, D}]}]
//		[INITIAL_CHECK [{ONFIRSTCONNECT | FIRSTUSE | DEFAULT}]
//		[PERSIST {TEMPORARY | MEMORY | DISK | DEFAULT}]
//		[ENABLED {YES | TRUE | NO | FALSE}]
//		[CALL_LIMIT {0 | -1 | max_calls}]
//		[FILTER (expression)]
	ResultSet rs = makeResultSet("CREATE CACHE ON Account");
	getAllSFContact();

    }

    public void getAllSFContact() {
	try {

	    ResultSet rs = makeResultSet("SELECT TOP 5 ROWID, SYS_NAME FROM Account");
	    while (rs.next()) {
		int ROWID = rs.getInt("ROWID");
		String SYSNAME = rs.getString("SYS_NAME");
		System.out.println(ROWID + "      " + SYSNAME);

	    }
	} catch (SQLException ex) {
	    System.out.println("Lijst met accounts ophalen gefaald.");
	}
	closeConnectRst();

    }

    public String getServerNaam() {
	return ServerNaam;
    }

    public String getUsername() {
	return username;
    }

    public String getPasssword() {
	return passsword;
    }

    public void setServerNaam(String serverNaam) {
	ServerNaam = serverNaam;
    }

    public void setUsername(String username) {
	this.username = username;
    }

    public void setPasssword(String passsword) {
	this.passsword = passsword;
    }

    public ResultSet makeResultSet(String query) {

	try {
	    rst = stmt.executeQuery(query);
	} catch (SQLException e) {

	    e.printStackTrace();
	}
	return rst;

    }

    public void connectUrlMaken() {

	String url = "jdbc:datadirect:sforce://login.salesforce.com;User=info@incentro.com;Password=Kampioenen2013PeQqjnySnw6f5J5EiqcS4Js3";

	try {
	    con = DriverManager.getConnection(url);
	    stmt = con.createStatement();
	    System.out.println("Verbinding!!!!!!!!");
	} catch (SQLException e) {
	    System.out.println("Verbinding kan niet gemaakt worden.");
	    e.printStackTrace();
	}

    }

    public void closeConnectRst() {
	try {
	    rst.close();
	    stmt.close();
	    con.close();
	} catch (SQLException e) {
	    e.printStackTrace();
	}
    }

}
